/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-09-22
 * Time: 17:03
 */
public class Solution {
    public String longestCommonPrefix(String[] strs) {
        //处理边界
        if (strs == null || strs.length == 0) {
            return "";
        }
        if (strs.length == 1){
            return strs[0];
        }

        String publicPart = strs[0];
        for (int i = 1; i < strs.length; i++) {
            //当公共前缀部分==0时，退出循环
            if (publicPart.length() == 0) {
                break;
            }

            String str = strs[i];
            int length = Math.min(str.length(),publicPart.length());
            StringBuilder s = new StringBuilder();
            for (int j = 0; j < length; j++) {
                char ch1 = publicPart.charAt(j);
                char ch2 =str.charAt(j);
                if (ch1 == ch2) {
                    s.append(ch1);
                }else {
                    break;
                }
            }
            publicPart = s.toString();
        }
        return publicPart;
    }
}
